package xiaocao.learn.designmodel.iterator;

public class GenericArrayList<E>{
	private Object[] objects = new Object[10];
	private int index = 0;
	public void add(E o){
		if(index == objects.length){
			Object[] newObjects = new Object[objects.length*2];
			System.arraycopy(objects, 0, newObjects, 0, objects.length);
			objects = newObjects;
		}
		objects[index] = o;
		index++;
	}
	public int size(){
		return index;
	}
	public Iterator iterator(){
		return new ArrayListIterator();
	}
	private class ArrayListIterator implements Iterator{
        private int currentIndex = 0;
		@Override
		public Object next() {
			Object o =  objects[currentIndex];
			currentIndex++;
			return o;
		}

		@Override
		public boolean hasNext() {
			if(currentIndex >= index){
				return false;
			}
			return true;
		}
		
	}
	public static void main(String args[]){
		GenericArrayList<String> list = new GenericArrayList<String>();
		list.add("xiaocao");
	}

}
